﻿<%@ WebHandler Language="C#" Class="banhezhanribao_chart" %>

using System;
using System.Web;
using System.Data;
using KeaoBase;
using ServiceStack.Redis;
using System.Configuration;
using System.Collections.Generic;
using System.Text;

public class banhezhanribao_chart : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.QueryString["uid"];
            string kid = context.Request.QueryString["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("{\"x\":[],\"data1\":[],\"data2\":[]}");
                return;
            }
            string key = KeaoUtils.GetMD5Str(context.Request.Url.OriginalString.Replace(uid,"cache").Replace(kid,"cache"));
            using (IRedisClient redis = RedisClientPool.GetReadOnlyRedisClient())
            {
                if (redis != null)
                {
                    if (redis.ContainsKey(key))
                    {
                        context.Response.Write(redis.GetValue(key));
                        return;
                    }
                }
            }
            string zhanDian = context.Request.QueryString["zhandian"];
            string kaiShiShiJian = context.Request.QueryString["kaishishijian"];
            string jieShuShiJian = context.Request.QueryString["jieshushijian"];
            if (string.IsNullOrEmpty(zhanDian)||string.IsNullOrEmpty(kaiShiShiJian)||string.IsNullOrEmpty(jieShuShiJian))
            {
                context.Response.Write("{\"x\":[],\"data1\":[],\"data2\":[]}");
                return;
            }
            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("{\"x\":[],\"data1\":[],\"data2\":[]}");
                return;
            }
            else
            {
                DateTime kaiShiDate = DateTime.Parse(kaiShiShiJian);
                DateTime jieShuDate = DateTime.Parse(jieShuShiJian).AddHours(23).AddMinutes(59).AddSeconds(59);
                string createCondition = string.Empty;
                if (zhanDian != "—全部—")
                {
                    createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDian, "", SearchConditionType.等于);
                }
                if (!string.IsNullOrEmpty(kaiShiShiJian) && !string.IsNullOrEmpty(jieShuShiJian))
                {
                    createCondition += m_DB.CreateAndCondition("shijian", DataValueCommonType.日期时间, kaiShiDate.ToString("yyyy-MM-dd HH:mm:ss"), jieShuDate.ToString("yyyy-MM-dd HH:mm:ss"), SearchConditionType.between);
                }
                Dictionary<string, string> orderBy = new Dictionary<string, string>();
                orderBy.Add("riqi", "asc");
                DataTable Dt = m_DB.GetDataTableByCondition("tbl_banhezhan", string.Format("{0} as riqi,sum(gl1) as gl1,sum(gl2) as gl2,sum(gl3) as gl3,sum(gl4) as gl4,sum(gl5) as gl5,sum(fl) as fl,sum(shui) as shui,sum(gl1+gl2+gl3+gl4+gl5+fl+shui) as hanshuizongzhong,sum(gl1+gl2+gl3+gl4+gl5+fl) as buhanshuizongzhong", m_DB.DateFormatFun("shijian")), null, createCondition, m_DB.DateFormatFun("shijian"), orderBy);

                if (Dt.Rows.Count > 0)
                {
                    StringBuilder xAxisBuilder = new StringBuilder();
                    StringBuilder data1Builder = new StringBuilder();
                    StringBuilder data2Builder = new StringBuilder();
                    xAxisBuilder.Append("\"x\":[");
                    data1Builder.Append("\"data1\":[");
                    data2Builder.Append("\"data2\":[");
                    while (kaiShiDate < jieShuDate)
                    {

                        string kaiShiDateStr = kaiShiDate.ToString("yyyy-MM-dd");
                        xAxisBuilder.Append(string.Format("\"{0}\",", kaiShiDateStr));
                        bool hasData = false;
                        foreach (DataRow row in Dt.Rows)
                        {
                            if (row["riqi"].Equals(kaiShiDateStr))
                            {
                                data1Builder.Append(string.Format("{0},", row["hanshuizongzhong"]));
                                data2Builder.Append(string.Format("{0},", row["buhanshuizongzhong"]));
                                hasData = true;
                                break;
                            }
                        }
                        if (!hasData)
                        {
                            data1Builder.Append("0,");
                            data2Builder.Append("0,");
                        }
                        kaiShiDate = kaiShiDate.AddDays(1);
                    }

                    xAxisBuilder.Remove(xAxisBuilder.Length - 1, 1);
                    data1Builder.Remove(data1Builder.Length - 1, 1);
                    data2Builder.Remove(data2Builder.Length - 1, 1);
                    xAxisBuilder.Append("]");
                    data1Builder.Append("]");
                    data2Builder.Append("]");
                    string responseJson = string.Format("{3}{0},{1},{2}{4}", xAxisBuilder.ToString(), data1Builder.ToString(), data2Builder.ToString(), "{", "}");
                    using (IRedisClient redis = RedisClientPool.GetRedisClient())
                    {
                        if (redis != null)
                        {
                            redis.SetValue(key, responseJson, TimeSpan.FromMinutes(10.0));
                        }
                    }
                    context.Response.Write(responseJson);
                }
                else
                {
                    context.Response.Write("{\"x\":[],\"data1\":[],\"data2\":[]}");
                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("查询拌合站日报图表异常。{0}", err.Message));
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}